var start = 1;
var limit = 3;
mui.init();

var item;
mui.plusReady(function() {
	// 监听网络状态变化
	initData();
	initListener();
});

function initData() {
	// 初始化事件
	var sss = {
		"start": 1,
		"limit": 1,
		"state": 0
	}
	ajaxF1(GETEVENT, sss, function(result) {
		console.log(JSON.stringify(result));
		var data = result.resultValue;
		//获取未处理完成的事件
		if(data && data.length > 0) {
			$("#ycts").attr("data-ID", data[0].ID);
			item = data[0];
			var date1 = Date.now(); //开始时间
			var date2 = new Date(data[0].LOGINDATE); //结束时间
			var date3 = date1 - date2.getTime(); //时间差的毫秒数
			if(date3 > 0) {
				$("#ycts").html('【异常提醒】:' + data[0].SJMS);
				var leave1 = date3 / (3600 * 1000) //计算天数后剩余的毫秒数
				var hours = Math.floor(leave1);
				$("#ycfssj").html(hours + "小时前");
			}
		}
	}, function() {
		mui.toast("请求失败");

	});
	initGccount();

	/*
	 * 工程列表初始化
	 */

	initBiaoduan();

	/*
	 * 获取水灰比
	 */
	ajaxF1(GETSHB, {}, function(result) {
		console.log(JSON.stringify(result));
		var data = result.resultValue;
		setSHB(data);
	}, function() {
		mui.toast("请求失败");

	});

}

window.addEventListener("reloadView", function() {
	start = 1;
	$("#ul_content").html("");
	initData();

})

function initListener() {
	$("#ul_content").on("tap", "li", function() {
		jumpTo('../biaoduan/biaoduan.html', 'biaoduan.html', jumpOptionFadein, {
			"ID": $(this).attr("ID")
		})
	});

	$("#sgtz").on("tap", function() {
		jumpTo("../konwledge/fxzsk.html", "fxzsk.html", jumpOptionFadein, {
			"type": "2001101"
		});

	});
	$("#jlgf").on("tap", function() {
		/*
		 * 监理规范
		 */
		jumpTo("../konwledge/fxzsk.html", "fxzsk.html", jumpOptionFadein, {
			"type": "2001102"
		});

	})
	$("#wtgl").on("tap", function() {
		/*
		 * 问题记录单
		 */
		jumpTo("../locat/locat.html", "locat.html", jumpOptionFadein, {

		});

	});
	/*
	 * 事件提醒
	 */
	$(".mui-table-view.sjtx").on("tap", function() {
		/*
		 * 问题记录单详情
		 */
		jumpTo("../locat/eventdetail/eventdetail.html", "eventdetail.html", jumpOptionFadein, {
			"ID": $("#ycts").attr("data-ID"),
			"item": JSON.stringify(item)
		});

	});
	mui("#div_addMore")[0].addEventListener("tap", function() {
		initBiaoduan();
	});

}

function initGccount() {
	//统计工程数量
	var sss = {
		guid: "111"
	}
	ajaxF1(GETBIAODAOCOUNT, {}, function(result) {
		console.log(JSON.stringify(result));
		var data = result.resultValue;
		//获取未处理完成的事件
		if(data && data.length > 0) {
			$("#zbdsl").html(data[0].BDZS);
			$("#zxzsl").html(data[0].ZXZBD);
			$("#dzxsl").html(data[0].WKZBD);
		}

	}, function() {
		mui.toast("请求失败");

	});
}

function initBiaoduan() {
	//初始化标段 GETBDLIST
	plus.nativeUI.showWaiting();
	ajaxF1(GETBDLIST, {
		start: start,
		limit: limit
	}, function(result) {
		console.log(JSON.stringify(result));
		var $myOrder11 = $("#mylist").find('li').eq(0);
		var data = result.resultValue;
		for(var i in data) {
			var $myOrder = $myOrder11.clone();
			$myOrder.attr("ID", data[i].ID);
			$myOrder.find(".center span").html("标段" + data[i].BD);
			$myOrder.find(".div_message_left .div_title").html(data[i].GCNAME + data[i].BDNAME);
			$myOrder.find(".div_message_left .div_nr").html(data[i].SGBZ);
			$myOrder.find(".ywcsl").html(data[i].YWCSGC);
			$myOrder.find(".zxzsl").html(data[i].SGZSGC);
			$myOrder.find(".wkzsl").html(data[i].WKZSGC);
			$("#ul_content").append($myOrder);
		}
		if(data && data.length === limit) {
			start += 1;
			if(mui("#div_addMore")[0].classList.contains("mui-hidden")) {
				mui("#div_addMore")[0].classList.remove("mui-hidden")
			}
		} else {
			if(!mui("#div_addMore")[0].classList.contains("mui-hidden")) {
				mui("#div_addMore")[0].classList.add("mui-hidden")
			}
		}
		plus.nativeUI.closeWaiting();
	}, function() {
		mui.toast("请求失败");
		plus.nativeUI.closeWaiting();

	});

}